﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XWG.JWBST.ProcessInterfaces;
using System.IO;
using System.Web;
using Newtonsoft.Json;
using XWG.Framework.AuthUtility;
using System.Data;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;

namespace XWG.JWBST.AnnouncementProcess
{
    /// <summary>
    /// 通知公告
    /// </summary>
    public class Process : IProcess
    {
        public string ProcessData(StreamReader reader, HttpContext context)
        {
            int Num = 15;  //每页显示的条数
            string json = string.Empty;
            string info = reader.ReadToEnd();
            info = HttpUtility.UrlDecode(info);
            JObject oka = (JObject)JsonConvert.DeserializeObject(info);
            string type = oka["processtype"].ToString().Replace("\"", string.Empty);
            //请求页数
            if (type == "GetPage")
            {
                int PageNum = 0;
                var Page = XWG.JWBST.JWBSTBLL.TbBstNoticeBLL.SelectList();
                if (Page.Count % Num == 0)
                {
                    PageNum = Page.Count / Num;
                }
                else   
                {
                    PageNum = (Page.Count / Num) + 1;
                }
                
                return PageNum.ToString();
            }
            //获取标题
            else if (type == "Title")
            {
                #region 分页 获取Title
                int pagenum = Convert.ToInt32(oka["pagenum"].ToString().Replace("\"", string.Empty));
                DataTable dtTbBstFastprocess = new DataTable();
                QueryByPage Q = new QueryByPage();
                dtTbBstFastprocess = Q.GetRecordByPage("ID,Title", "where deltag = 0 and auditsign = 1", "ISTOP,CREATEDATE", Num, pagenum, "TB_BST_NOTICE", "ID", "DESC").Tables[0];
                json = JsonConvert.SerializeObject(dtTbBstFastprocess);
                return json;
                #endregion
            }
            else
            {
                string id = oka["ID"].ToString().Replace("\"", string.Empty);
                IsoDateTimeConverter dtConverter = new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-dd HH:mm:ss" };
                json = JsonConvert.SerializeObject(XWG.JWBST.JWBSTBLL.TbBstNoticeBLL.Load(id),dtConverter);
                return json;
            }
        }
    }
}
